ContractForm = Ext
		.extend(
				Ext.Window,
				{
					formPanel : null,
					constructor : function(a) {
						Ext.applyIf(this, a);
						this.initUIComponents();
						ContractForm.superclass.constructor.call(this, {
							layout : "fit",
							id : "ContractFormWin",
							title : "合同详细信息",
							iconCls : "menu-contract",
							width : 915,
							height : 500,
//							minWidth : 850,
//							minHeight : 550,
							
							items : this.formPanel,
							border : false,
							modal : true,
							plain : true,
							buttonAlign : "center",
							buttons : this.buttons
						});
					},
					initUIComponents : function() {
						
						dataa=[];
						vstore = new Ext.data.JsonStore({
							data:dataa,
							fields:["fileId","fileName","filePath"]
						});
						var e = new Ext.grid.CheckboxSelectionModel();
						msgrid1 = new Ext.grid.GridPanel({
							id:'contractFileNamePanel',
							store:vstore,
							height:170,
							autoScroll : true,
							sm : e,
							columns:[e,{header:"图片名称",dataIndex:"fileName",width:498}]
						});
						msgrid1.addListener("rowdblclick", function(c,
								f, d) {
							c.getSelectionModel().each(function(e) {
								FileUploadImageDetailForm.show(e.data.fileId);
							});
						});
						var b = __ctxPath + "/system/listDepartment.do";
						var a = new TreeSelector("serviceDepSelector", b,
								"维护部门", null);
						a.addListener("change", function() {
							Ext.getCmp("serviceDep").setValue(a.getValue());
						});						
						var c = new ContractConfigView();
						var cc=new ContractTechniqueView();
						
						this.formPanel = new Ext.FormPanel(
								{
									url : __ctxPath
											+ "/contract/saveContract.do",
									layout : "hbox",
									frame : false,
									layoutConfig : {
										padding : "5",
										pack : "start",
										align : "middle"
									},
									defaults : {
										margins : "0 5 0 5"
									},
									id : "ContractForm",
									frame : false,
									formId : "ContractFormId",
									defaultType : "textfield",
									autoScroll : true,
									items : [
											{
												xtype : "fieldset",
												title : "合同信息(带 * 号为必填)",
												layout : "form",
//												rowspan : 2,
												labelWidth : 80,
												defaultType : "textfield",
												width : 400,
												height : 680,
												defaults : {
													width : 280
												},
												items : [
														{
															name : "contract.contractId",
															id : "contractId",
															xtype : "hidden",
															value : this.contractId == null ? ""
																	: this.contractId
														},
														{
															xtype : "hidden",
															name : "contract.ctFilePath",
															id : "ctFilePath"
														},
														{
															fieldLabel : "合同编号*",
															name : "contract.contractNo",
															id : "contractNo",
															allowBlank : false,
															blankText : "合同编号不可为空!"
														},
														{
															fieldLabel : "合同标题*",
															name : "contract.contractName",
															id : "contractName",
															allowBlank : false,
															blankText : "合同标题不可为空!"
														},
														{
															xtype : "compositefield",
															fieldLabel : "合同金额*",
															items : [
																	{
																		xtype : "numberfield",
																		width : 230,
																		name : "contract.contractAmount",
																		id : "contractAmount",
																		allowBlank : false,
																		blankText : "合同金额不可为空!"
																	},
																	{
																		value : "元",
																		xtype:"displayfield",
																		width : 10,
																		
																		style : "padding-left:0px;padding-top:3px;"
																	} ]
														},
														
														{
															fieldLabel : "合同类型",
															xtype : "textfield",
															name : "contract.contractType",
															id : "contractType"
														},
														{
															fieldLabel : "项目编号",
															xtype : "textfield",
															name : "contract.projectNo",
															id : "projectNo"
														},
														{
															fieldLabel : "项目名称",
															xtype : "textfield",
															name : "contract.projectName",
															id : "projectName"
														},
														{
															fieldLabel : "主要条款",
															xtype : "textarea",
															name : "contract.mainItem",
															id : "mainItem"
														},
														{
															fieldLabel : "售后条款",
															xtype : "textarea",
															name : "contract.salesAfterItem",
															id : "salesAfterItem"
														},
														{
															xtype : "compositefield",
															fieldLabel : "起止日期*",
															items : [
																	{
																		xtype : "datefield",
																		width : 110,
																		format : "Y-m-d",
																		editable : true,
																		name : "contract.validDate",
																		id : "validDate",
																		allowBlank : false,
																		blankText : "合同生效日期不可为空!"
																	},
																	{
																		value : "至",
																		xtype : "displayfield",
																		width : 10,
																		style : "padding-left:0px;padding-top:3px;"
																	},
																	{
																		xtype : "datefield",
																		width : 110,
																		format : "Y-m-d",
																		editable : true,
																		name : "contract.expireDate",
																		id : "expireDate",
																		allowBlank : false,
																		blankText : "合同到期日期不可为空!"
																	} ]
														},
														{
															xtype : "compositefield",
															fieldLabel : "签定人*",
															items : [
																	{
																		xtype : "textfield",
																		width : 180,
//																		readOnly : true,
																		name : "contract.signupUser",
																		id : "signupUser",
																		editable : true,
																		allowBlank : false,
																		blankText : "签定人不可为空!"
																	},
																	{
																		xtype : "button",
																		text : "签定人",
																		iconCls : "btn-mail_recipient",
																		handler : function() {
																			UserSelector
																					.getView(
																							function(
																									e,
																									d) {
																								Ext
																										.getCmp(
																												"signupUser")
																										.setValue(
																												d);
																							},
																							true)
																					.show();
																		}
																	} ]
														},
														{
															fieldLabel : "签定时间",
															xtype : "datefield",
															format : "Y-m-d",
															editable : true,
															name : "contract.signupTime",
															id : "signupTime",
															allowBlank : false,
															blankText : "签定时间不可为空!"
														},
														{
															fieldLabel : "维护部门",
															name : "contract.serviceDep",															
															xtype : "hidden",
															id : "serviceDep"
																
														},
														a,
													
														
														
														{
															xtype : "compositefield",
															fieldLabel : "维护人*",
															items : [
																	{
																		xtype : "textfield",
																		width : 180,
//																		readOnly : true,
																		name : "contract.serviceMan",
																		editable : true,
																		id : "serviceMan"
																	},
																	{
																		xtype : "button",
																		text : "维护人",
																		iconCls : "btn-mail_recipient",
																		handler : function() {
																			UserSelector
																					.getView(
																							function(
																									e,
																									d) {
																								Ext
																										.getCmp(
																												"serviceMan")
																										.setValue(
																												d);
																							},
																							true)
																					.show();
																		}
																	} ]
														},
//														{
//															xtype : "compositefield",
//															fieldLabel : "合同文件",
//															items : [
//																	{
//																		xtype : "panel",
//																		id : "displayContractAttach",
//																		width : 220,
//																		height : 50,
//																		
//																	},
//																	{
//																		xtype : "button",
//																		iconCls : "btn-upload",
//																		text : "上传",
//																		handler : function() {
//																			var d = App
//																					.createUploadDialog({
//																						permitted_extensions : [ "zip", "jasper", "jpg"],
//																						file_cat : "contracts/contractFile",
//																						callback : uploadContractAttach
//																					});
//																			d
//																					.show("queryBtn");
//																		}
//																	}
//																	 ]
//														}
														
														
														{

															id : "displayContractAttach",
															xtype : "panel",
//															bodyStyle : "padding:0px;",
															title : "上传合同",
															width : 380,
															autoHeight : true,														
															border : false,
															items : msgrid1,
//															html : '<div align="center"><img src="'
//																	+ __ctxPath
//																	+ '/images/no_image.jpg" style=""/></div>',
															tbar : new Ext.Toolbar(
																	{
																		height : 30,
																		items : [
																				{
																					text : "选择图片",
																					iconCls : "btn-upload",
																					handler : function() {
																						ContractForm
																								.uploadPhotoBtn();
																					}
																				},
																				{
																					text : "删除",
																					iconCls : "btn-delete",
																					handler : function() {
																						ContractForm
																								.deletePhotoBtn();
																					}
																				} ]
																	})

														}
														
														
														]
											},
											{
												xtype : "container",
												style : "padding:5px 0px 0px 0px;",
												items : [
												         
														{
															xtype : "fieldset",
															title : "工艺文件信息",
															layout : "form",
//															labelWidth : 55,
															width : 465,
															autoHeight : true,
															style : "padding-bottom:0px;bottom:0px;",
															defaultType : "textfield",
															items : [
															         cc
//																	{
//																		fieldLabel : "所属项目",
//																		xtype : "hidden",
//																		name : "contract.projectId",
//																		id : "projectId"
//																	},
//																	{
//																		xtype : "button",
//																		iconCls : "menu-project",
//																		text : "选择项目",
//																		handler : function() {
//																			ProjectSelector
//																					.getView(
//																							function(
//																									f,
//																									e,
//																									d) {
//																								Ext
//																										.getCmp(
//																												"projectId")
//																										.setValue(
//																												f);
//																								ContractForm
//																										.getProject(f);
//																							},
//																							true)
//																					.show();
//																		}
//																	},
//																	{
//																		xtype : "panel",
//																		id : "ProjectDisplay",
//																		autoWidth : true,
//																		height : 130,
//																		autoScroll : true,
//																		html : "项目编号: <br> 项目名称: <br> 所属客户: <br>"
//																				+ "联系人员: <br> 项目描述: <br>"
//																	}
																	
																	]
														},
														{
															xtype : "fieldset",
															title : "产品交付信息",
															layout : "form",
															width : 465,
															style : "padding-top:0px;top:0px;",
															autoHeight : true,
															labelWidth : 58,
															defaultType : "textfield",
															items : [
																	{
																		fieldLabel : "收货地址",
																		name : "contract.consignAddress",
																		id : "consignAddress",
																		width : 280
																	},
																	{
																		fieldLabel : "收货人",
																		name : "contract.consignee",
																		id : "consignee",
																		width : 280
																	}, c ]
														} ]
											} ]
								});
						if (this.contractId != null
								&& this.contractId != "undefined") {
							this.formPanel
									.getForm()
									.load(
											{
												deferredRender : false,
												url : __ctxPath
														+ "/contract/getContract.do?contractId="
														+ this.contractId,
												waitMsg : "正在载入数据...",
												success : function(e, g) {
													var p = g.result.data;
//													var gg = Ext.util.JSON.decode(e.responseText).data;
//													if (gg) {
//														try{
//															contractFormUploadUserPhoto(p.ctFilePath,1);
//														}catch(e){
//																
//														}
//													}
													if(p.ctFilePath){
														contractFormUploadUserPhoto(p.ctFilePath,1);
													}
													var h = getDateFromFormat(
															p.validDate,
															"yyyy-MM-dd HH:mm:ss");
													var d = getDateFromFormat(
															p.expireDate,
															"yyyy-MM-dd HH:mm:ss");
													var k = getDateFromFormat(
															p.signupTime,
															"yyyy-MM-dd HH:mm:ss");
													Ext
															.getCmp("validDate")
															.setValue(
																	new Date(h));
													Ext
															.getCmp(
																	"expireDate")
															.setValue(
																	new Date(d));
													Ext
															.getCmp(
																	"signupTime")
															.setValue(
																	new Date(k));
													Ext
															.getCmp(
																	"serviceDepSelector")
															.setValue(
																	p.serviceDep);
//													Ext
//															.getCmp("projectNo")
//															.setValue(
//																	g.result.projectNo);
//													uploadContractAttach(p.ctFilePath);
													
													var f = Ext
															.getCmp("contractId").value;
													if (f != null && f != ""
															&& f != "undefined") {
														var z = Ext
														.getCmp(
																"ContractTechniqueGrid")
														.getStore();
												z
														.reload({
															params : {
																"Q_contract.contractId_L_EQ" : f
															}
														});
														
														var n = Ext
																.getCmp(
																		"ContractConfigGrid")
																.getStore();
														n
																.reload({
																	params : {
																		"Q_contract.contractId_L_EQ" : f
																	}
																});
													}
//													var m = g.result.data.contractFiles;
//													var l = Ext
//															.getCmp("displayContractAttach");
//													var o = Ext
//															.getCmp("ctFileId");
//													for (var j = 0; j < m.length; j++) {
//														if (o.getValue() != "") {
//															o.setValue(o
//																	.getValue()
//																	+ ",");
//														}
//														o.setValue(o.getValue()
//																+ m[j].fileId);
//														Ext.DomHelper
//																.append(
//																		l.body,
//																		'<span><a href="#" onclick="FileAttachDetail.show('
//																				+ m[j].fileId
//																				+ ')">'
//																				+ m[j].fileName
//																				+ '</a><img class="img-delete" src="'
//																				+ __ctxPath
//																				+ '/images/system/delete.gif" onclick="removeContractAttach(this,'
//																				+ m[j].fileId
//																				+ ')"/>&nbsp;|&nbsp;</span>');
//													}
												},
												failure : function(d, e) {
													Ext.ux.Toast.msg("编辑",
															"载入失败");
												}
											});
						}
						this.buttons = [ {
							text : "保存",
							iconCls : "btn-save",
							handler : function() {
								ContractForm.saveContract();
							}
						}, {
							text : "取消",
							iconCls : "btn-cancel",
							handler : function() {
								Ext.getCmp("ContractFormWin").close();
							}
						} ];
					}
				});
//ContractForm.getProject = function(a) {
//	if (a != null && a != "" && a != "undefined") {
//		Ext.Ajax.request({
//			url : __ctxPath + "/customer/getProject.do",
//			params : {
//				projectId : a
//			},
//			method : "post",
//			success : function(c) {
//				var b = Ext.util.JSON.decode(c.responseText);
//				Ext.getCmp("ProjectDisplay").body.update("项目编号: "
//						+ b.data.projectNo + "<br> 项目名称: " + b.data.projectName
//						+ "<br> 所属客户: " + b.customerName + "<br> 联系人员: "
//						+ b.data.fullname + "<br> 项目描述: " + b.data.reqDesc);
//			}
//		});
//	}
//};
ContractForm.saveContract = function() {
	var c = Ext.getCmp("ContractForm");
	//取得零部件配置信息中的数据
	var b = Ext.getCmp("ContractConfigGrid").getStore();
	var d = [];
	for (i = 0, cnt = b.getCount(); i < cnt; i += 1) {
		var a = b.getAt(i);
		if (a.data.itemName == null || a.data.itemName == ""
				|| a.data.itemName == "undefined") {
			Ext.ux.Toast.msg("提示信息", "零部件名称为必填!");
			return;
		}
		if (a.data.itemSpec == null || a.data.itemSpec == ""
				|| a.data.itemSpec == "undefined") {
			Ext.ux.Toast.msg("提示信息", "规格为必填!");
			return;
		}
		if (a.data.amount == null || a.data.amount == ""
				|| a.data.amount == "undefined") {
			Ext.ux.Toast.msg("提示信息", "数量为必填!");
			return;
		}
		if (a.data.assignId == "" || a.data.assignId == null) {
			a.set("configId", -1);
		}
		if (a.dirty) {
			d.push(a.data);
		}
	}
	
//	取得附带工艺信息中的数据
	var bb = Ext.getCmp("ContractTechniqueGrid").getStore();
	var dd = [];
	for (i = 0, cnt = bb.getCount(); i < cnt; i += 1) {
		var aa = bb.getAt(i);
		if (aa.data.dwgNo == null || aa.data.dwgNo == ""
				|| aa.data.dwgNo == "undefined") {
			Ext.ux.Toast.msg("提示信息", "图代号为必填!");
			return;
		}
		if (aa.data.batchNo == null || aa.data.batchNo == ""
				|| aa.data.batchNo == "undefined") {
			Ext.ux.Toast.msg("提示信息", "批次号为必填!");
			return;
		}
		if (aa.data.orderNo == null || aa.data.orderNo == ""
				|| aa.data.orderNo == "undefined") {
			Ext.ux.Toast.msg("提示信息", "顺序号为必填!");
			return;
		}
		if (aa.data.versionNo == null || aa.data.versionNo == ""
			|| aa.data.versionNo == "undefined") {
		Ext.ux.Toast.msg("提示信息", "版本号为必填!");
		return;
	}
		if (aa.data.assignId == "" || aa.data.assignId == null) {
			aa.set("configId", -1);
		}
		if (aa.dirty) {
			dd.push(aa.data);
		}
	}
	
	
	 var a = Ext.getCmp("ctFilePath");
	 var bf="[";	
	 for(var i=0;i<msgrid1.getStore().getCount();i++){
		// var bf=new RType({fileId:msgrid.getStore().getAt(i).get('fileId'),fileName:msgrid.getStore().getAt(i).get('fileName'),filePath:msgrid.getStore().getAt(i).get('filePath')});
		// msgrid.getStore().getAt(i).get('fileId');
		 bf+="{fileId:'"+msgrid1.getStore().getAt(i).get('fileId')+"',fileName:'"+msgrid1.getStore().getAt(i).get('fileName')+"',filePath:'"+msgrid1.getStore().getAt(i).get('filePath')+"'}";
		 if (i!=msgrid1.getStore().getCount()-1) bf+=",";
	 }
	 a.setValue(bf+"]");
	
	
		
	
	
	if (c.getForm().isValid()) {
		c.getForm().submit({
			method : "post",
			params : {
				contractConfigs : Ext.encode(d),
				contractTechniques:Ext.encode(dd)
			},
			waitMsg : "正在提交数据...",
			success : function(e, f) {
				Ext.ux.Toast.msg("操作提示", "成功保存信息！");
				Ext.getCmp("ContractGrid").getStore().reload();
				Ext.getCmp("ContractFormWin").close();
			},
			failure : function(e, f) {
				Ext.MessageBox.show({
					title : "操作提示",
					msg : f.result.msg,
					buttons : Ext.MessageBox.OK,
					icon : "ext-mb-error"
				});
			}
		});
	}
};
//function uploadContractAttach(c) {
//	var a = Ext.getCmp("ctFilePath");
//	var d = Ext.getCmp("displayContractAttach");
//	
//
//	for (var b = 0; b < c.length; b++) {
//		if (a.getValue() != "") {
//			a.setValue(a.getValue() + ",");
//		}
//		a.setValue(a.getValue() + c[b].filePath);
//		Ext.DomHelper
//				.append(
//						d.body,
//						'<span id="fpath"><a href="#" onclick="FileAttachDetail.show('
//								+ c[b].fileId
//								+ ')">'
//								+ c[b].fileName
//								+ '</a><img class="img-delete" src="'
//								+ __ctxPath
//								+ '/images/system/delete.gif" onclick="removeContractAttach(this,'
//								+ c[b].fileId + ')"/></span>');
//		
//	}
//	
//}
//function removeContractAttach(f, d) {
//	var b = Ext.getCmp("ctFilePath");
//	var e = b.getValue();
//	if (e.indexOf(",") < 0) {
//		b.setValue("");
//	} else {
//		e = e.replace("," + d, "").replace(d + ",", "");
//		b.setValue(e);
//	}
//	var c = Ext.get(f.parentNode);
//	c.remove();
//	var a = Ext.getCmp("ctFilePath").value;
//	if (a != null && a != "" && a != "undefined") {
//		Ext.Ajax.request({
//			url : __ctxPath + "/customer/removeFileContract.do",
//			params : {
//				contractId : a,
//				ctFilePath : d
//			},
//			method : "post",
//			success : function() {
//						Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");		
//			}
//		});
//
//	}
//}
ContractForm.uploadPhotoBtn = function() {
	var a = Ext.getCmp("ctFilePath");
	var b = App.createUploadDialog({
		file_cat : "contracts/contractFile",
		callback : contractFormUploadUserPhoto,
		permitted_extensions : [ "jpg" ]
	});
	b.show("queryBtn");
};
ContractForm.deletePhotoBtn = function() {
	var records = msgrid1.getSelectionModel().getSelections();
	for(var i = 0,len = records.length;i<len;i++ ){
	        msgrid1.store.remove(records[i]);
	}
    /*var a = Ext.getCmp("productionProcesses.ppArtWorkSheetImg");
	a.setValue("");
	var e = Ext.getCmp("ppArtWorkSheetImg");
	e.body.update('<img src="' + __ctxPath
			+ '/images/no_image.jpg"  width="100%" height="100%"/>');*/
	};
function contractFormUploadUserPhoto(b,j) {
	var a = Ext.getCmp("ctFilePath");
	try{var h=Ext.getCmp("contractId").getValue();}catch(e){}
	var c = Ext.getCmp("displayContractAttach"); 
	if(h==""&&j==1){
		for(var i in b){		
			if(b[i].filePath != undefined){
				 var RType=Ext.data.Record.create([
				                                {name:"fileId",type:"string"},
				                                {name:"fileName",type:"string"},
				                                {name:"filePath",type:"string"}
				                               ]);
				
				 var bf=new RType({fileId:b[i].fileId,fileName:b[i].fileName,filePath:b[i].filePath});
				 msgrid1.getStore().add(bf);
			}
		}
		 
	}else{
		if(msgrid1.getStore().getCount()==0){
			try{msgrid1.getStore().loadData(eval(a.getValue()),false);}catch(e){}
		  }
			 for(var i in b){ 
				 if(b[i].filePath != undefined){ 
					 var RType=Ext.data.Record.create([
					                                {name:"fileId",type:"string"},
					                                {name:"fileName",type:"string"},
					                                {name:"filePath",type:"string"}
					                               ]);
					
					 var bf=new RType({fileId:b[i].fileId,fileName:b[i].fileName,filePath:b[i].filePath});
					 var bff=new RType({fileName:b[i].fileName});
					 msgrid1.getStore().add(bf);										
				 }	
			}		
	 }
};